#include "memoQueryWidget.h"
#include "ui_memoQueryWidget.h"
#include "MemoGlobal.h"

MemoQueryWidget::MemoQueryWidget(QSqlDatabase _db,
                                 QWidget* parent)
    :m_SqlDB(_db),
    QWidget(parent),
    ui(new Ui::MemoQueryWidget)
{
    ui->setupUi(this);

    customUI();


}

MemoQueryWidget::~MemoQueryWidget()
{
    delete ui;
}

void MemoQueryWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MemoQueryWidget::customUI()
{
    // the label combox
    m_pComboxSqlQueryModel = new QSqlQueryModel;
    m_pComboxSqlQueryModel->setQuery("SELECT * FROM MemoState ORDER BY ID ASC", m_SqlDB);

    ui->ui_ComboBoxMemoStatus->setModel(m_pComboxSqlQueryModel);
    ui->ui_ComboBoxMemoStatus->setModelColumn(1);

    //Date Time
    ui->ui_DateTimeEditMemoDateAfter->setDateTime(QDateTime::currentDateTime().addDays((-1)*Memo::dDayStep));
    ui->ui_DateTimeEditMemoDateBefore->setDateTime(QDateTime::currentDateTime());

    //TableView
    //ui_TableViewQuery
    m_pTableViewModel = new QSqlQueryModel;
    ui->ui_TableViewQuery->setModel(m_pTableViewModel);
    ui->ui_TableViewQuery->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->ui_TableViewQuery->setSelectionBehavior(QAbstractItemView::SelectRows);


    //Btn search
    connect(ui->ui_BtnMemoSearch, SIGNAL(clicked()),
            this, SLOT(doBtnSearch()));

    //
    connect(ui->ui_TableViewQuery,SIGNAL(clicked(QModelIndex)),
            this, SLOT(doTableIndexActivated(QModelIndex)));
//    connect(ui->ui_TableViewQuery,SIGNAL(activated(QModelIndex)),
//            this, SLOT(doTableIndexActivated(QModelIndex)));
//    connect(ui->ui_TableViewQuery,SIGNAL(pressed(QModelIndex)),
//            this, SLOT(doTableIndexActivated(QModelIndex)));

}

void MemoQueryWidget::updateComboxMemoLabel()
{
    m_pComboxSqlQueryModel->setQuery("SELECT * FROM MemoState ORDER BY ID ASC", m_SqlDB);
}

void MemoQueryWidget::doBtnSearch()
{
    QStringList _strsContain;
    _strsContain.clear();

    //get contain from UIs
    if(ui->ui_CkeckBoxMemoTitle->isChecked()){
        QString _str = ui->ui_LineEditMemoTitle->text();
        _strsContain.append(QString(" MemoTitle LIKE '%%1%' ").arg(_str));
    }

    if(ui->ui_CheckBoxMemoStatus->isChecked()){
//        ui->ui_ComboBoxMemoStatus->currentIndex();
        QString _strStateID = this->m_pComboxSqlQueryModel->record(ui->ui_ComboBoxMemoStatus->currentIndex()).value("ID").toString();
        _strsContain.append(QString(" MomoStatus = '%1' ").arg(_strStateID));
    }


    if(ui->ui_CheckBoxMemoDateAfter->isChecked()){
        QString _strDateAfterCondition = tr(" MomoRecordDate >= '%1' ").arg(ui->ui_DateTimeEditMemoDateAfter->dateTime().toString(Memo::strDateTimeConvertFormat));
        _strsContain.append(_strDateAfterCondition);
    }

    if(ui->ui_CheckBoxMemoDateBefore->isChecked()){
        QString _strDateBeforeCondition = tr(" MomoRecordDate <= '%1' ").arg(ui->ui_DateTimeEditMemoDateBefore->dateTime().toString(Memo::strDateTimeConvertFormat));
        _strsContain.append(_strDateBeforeCondition);
    }

    //ActiveMomoView
    //or
    //ActiveMomoList
    QString _strQuery = QString("SELECT * FROM ActiveMomoView ");
    if(_strsContain.count()>0){
        _strQuery.append(" WHERE ");
        _strQuery.append(_strsContain.at(0));
        for(int i=1 ; i<_strsContain.count() ; i++){
            _strQuery.append(" AND ");
            _strQuery.append(_strsContain.at(i));

        }
    }

//    qDebug()<<_strQuery;
    this->m_pTableViewModel->setQuery(_strQuery, this->m_SqlDB);
    ui->ui_TableViewQuery->hideColumn(0);
    ui->ui_TableViewQuery->hideColumn(1);

//    emit doSearch();
    m_strActiveID = "";
    emit activated(m_strActiveID);
}

void MemoQueryWidget::doTableIndexActivated(QModelIndex index)
{
//    index.
    QString _strActiveID = m_pTableViewModel->record(index.row()).value("MemoID").toString();
    m_strActiveID = _strActiveID;
    emit activated(_strActiveID);
}
